package com.hzhr.extdevice.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = {"com.hzhr.extdevice.mapper.toll"}, sqlSessionTemplateRef = "sqlSessionTemplateToll")
public class MybatisConfig_Toll
{
    @Bean(name = {"dataSourceToll"})
    @ConfigurationProperties(prefix = "spring.datasourceToll")
    public DataSource dataSource() { return new DruidDataSource(); }


    @Bean(name = {"sqlSessionFactoryToll"})
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourceToll") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations((new PathMatchingResourcePatternResolver()).getResources("classpath:mybatis/mapper/toll/*.xml"));
        return bean.getObject();
    }


    @Bean(name = {"transactionManagerToll"})
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSourceToll") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }



    @Bean(name = {"sqlSessionTemplateToll"})
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryToll") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
}
